package com.shundr.auth.frame.constant;

import android.content.Context;
import android.content.SharedPreferences;

import com.shundr.auth.common.model.AreaInfo;
import com.shundr.auth.common.view.SharedPrefUtil;
import com.shundr.auth.frame.util.StringUtil;
import com.shundr.auth.user.dao.UserDao;
import com.shundr.auth.user.model.UserInfo;

public class LContext {

	/*********** 全局变量 **************/

	public static final int PAGE_SIZE = 10;
	public static final String nullStr = "null";

	public static Context context;
	public static String CLIENT_TYPE = "2000";
	private static String ACCESS_TOKEN = "";// 登录用户的验证机制
	private static String STAFF_TOKEN = "";// 登录管理员的验证机制

	public static AreaInfo areaInfo;// 当前位置

	public static int width;
	public static int height;

	public static UserInfo userInfo;// 当前客户用户

	public static UserInfo adminInfo;// 当前业务员用户

	private static SharedPrefUtil shPre;
	public static final String LAST_ACCESS_TOKEN = "last_access_token";
	public static final String LAST_STAFF_ACCESS_TOKEN = "last_staff_access_token";

	/**
	 * 
	 * getUserInfo(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param ctxt
	 * @return UserInfo
	 * @exception
	 * @since 1.0.0
	 */
	public static UserInfo getUserInfo(Context ctxt) {
		if (StringUtil.isNull(ACCESS_TOKEN)) {
			return null;
		} else {
			if (userInfo == null) {
				return new UserDao().getUserByToken(ACCESS_TOKEN);
			} else {
				return userInfo;
			}
		}
	}

	/**
	 * 判断用户是否已登录 isLogin(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @return boolean
	 * @exception
	 * @since 1.0.0
	 */
	public static boolean isLogin() {
		if (!StringUtil.isNull(ACCESS_TOKEN) && userInfo != null) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * init(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param context
	 *            void
	 * @exception
	 * @since 1.0.0
	 */
	public static void init(Context context) {
		if (context == null) {
			return;
		}
		LContext.context = context;
		shPre = new SharedPrefUtil(context, LConstants.PRE_NAME);
	}

	public static String getACCESS_TOKEN() {
		if (StringUtil.isNull(ACCESS_TOKEN)) {
			return shPre.getString(LAST_ACCESS_TOKEN, "");
		}
		return ACCESS_TOKEN;
	}

	public static void setACCESS_TOKEN(String aCCESS_TOKEN) {
		ACCESS_TOKEN = aCCESS_TOKEN;
		shPre.putString(LAST_ACCESS_TOKEN, aCCESS_TOKEN);
		shPre.commit();
	}

	public static String getSTAFF_TOKEN() {
		if (StringUtil.isNull(STAFF_TOKEN)) {
			return shPre.getString(LAST_STAFF_ACCESS_TOKEN, "");
		}
		return STAFF_TOKEN;
	}

	public static void setSTAFF_TOKEN(String sTAFF_TOKEN) {
		STAFF_TOKEN = sTAFF_TOKEN;
		shPre.putString(LAST_STAFF_ACCESS_TOKEN, sTAFF_TOKEN);
		shPre.commit();
	}
}
